<template>
  <view>
    <view class="goods-list">
      <my-goods v-for="(item,index) in goodsList" :key="index" :goods="item" @click="gotoDetail"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        queryObj: {
          query: '', //查询关键词
          cid: '', //分类ID
          pagenum: 1, //页数索引 
          pagesize: 10 //每页长度 
        },
        // 商品列表
        goodsList: [],
        // 总记录数
        total: 0,
        // 节流阀
        isLoading: false
      };
    },
    onLoad(opations) {
      this.queryObj.cid = opations.cid || ''
      this.queryObj.query = opations.query || ''
      
      this.getGoodsList()
    },
    methods: {
      async getGoodsList() {
        const { data: res } = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }

        this.total = res.message.total
        this.goodsList = [...this.goodsList,...res.message.goods]
      },
      gotoDetail(goods){
          uni.navigateTo({
            url:'/subpkg/goods_detail/goods_detail?goods_id='+ goods.goods_id
          })
      }
    },
    
    async onReachBottom() {
      if(this.queryObj.pagenum * this.queryObj.pagesize > this.total){
        return uni.$showMsg('数据加载完毕')
      }
      
      if(this.isLoading === true){
        return 
      }
      this.queryObj.pagenum += 1
      this.isLoading = true
      await this.getGoodsList()
      this.isLoading = false
    },
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.isLoading = false
      this.total = 0
      this.goodsList = []
      
      this.getGoodsList().then(() => uni.stopPullDownRefresh())
    }
  }
</script>

<style lang="scss">




</style>